package cn.com.inossem.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import cn.com.inossem.adapter.XiTongMessageListAdapter;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.model.MessageInfoBean;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.Utils;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 消息页
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class XiTongMessageActivity extends RequestActivity {

	ImageView back;
	ListView listview = null;
	XiTongMessageListAdapter adapter = null;
	public boolean resultflag = true;

	int page = 1;

	public List<MessageInfoBean> messageInfo;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 1:
				String friendid = "";
				friendid = msg.getData().getString("friendid");
				launchRequest(WooRequestFactory.agreefriend(friendid));
				break;

			default:
				break;
			}
		}

	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);
		Utils.messageInfo.userInfo.setMessageNum("0");
		initView();
		launchRequest(WooRequestFactory.getMessage(page + ""));
	}
	
	@Override
	public void onResume() {
		super.onResume();
		Utils.messageInfo.userInfo.setMessageNum("0");
	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		messageInfo = new ArrayList<MessageInfoBean>();

		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		listview = (ListView) this.findViewById(R.id.listView);

		listview.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				switch (scrollState) { // 当不滚动时
				case OnScrollListener.SCROLL_STATE_IDLE: // 判断滚动到底部
					if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
						if (resultflag) {
							resultflag = false;
							page++;
							launchRequest(WooRequestFactory.getMessage(page
									+ ""));
						}
					}
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub

			}
		});

	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {

		if (bundle.containsKey(WooRequestFactory.BUNDLE_EXTRA_SUCCESS)) {
			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_SUCCESS) == 1) {

				Utils.messageInfo.userInfo.setMessageNum("0");

				if (page == 1) {
					messageInfo = new ArrayList<MessageInfoBean>();
				}
				List<MessageInfoBean> retList = bundle
						.getParcelableArrayList(WooRequestFactory.BUNDLE_EXTRA_MESSAGELIST);
				if (retList.size() > 0) {
					messageInfo.addAll(retList);

					System.out.println("长度：" + messageInfo.size());
					if (adapter == null) {
						adapter = new XiTongMessageListAdapter(this,
								messageInfo, handler);
						listview.setAdapter(adapter);
						adapter.notifyDataSetChanged();
					} else {
						adapter.notifyDataSetChanged();
					}

				}
				if (adapter != null) {
					adapter.notifyDataSetChanged();
				}
			}
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS)) {
			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS) == 1) {
				// 添加成功
				adapter = null;
				page = 1;
				if (null != Utils.messageInfo) {
					int friendNum = Integer.valueOf(Utils.messageInfo.userInfo.getFriendNum());
					Utils.messageInfo.userInfo.setFriendNum(String.valueOf(friendNum + 1));
				}
				launchRequest(WooRequestFactory.getMessage(page + ""));
			} else if (bundle
					.getInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS) == 11) {
				Toast.makeText(this, "已是好友", Toast.LENGTH_LONG).show();
			}
		}

	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.xitongmessage_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			setResult(99);
			finish();
			break;

		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			setResult(1);
			finish();
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			setResult(99);
			finish();
			return true;

		}

		return super.onKeyDown(keyCode, event);

	}
}
